import {getLatestTime} from "@/services/indicator";

/**
 * Created by lifeng on 2019/7/13.
 */
export default {
  getStartOfDay() {
    const now = currentTime()
    const year = now.getFullYear()
    const month = now.getMonth()
    const day = now.getDate()
    return new Date(year, month, day)
  },
  getEndOfDay() {
    const now = currentTime()
    const year = now.getFullYear()
    const month = now.getMonth()
    const day = now.getDate()
    return new Date(year, month, day, 23, 59, 59)
  },

  plusDay(date, day) {
    return new Date(date.getTime() + day * 24 * 60 * 60 * 1000)
  },

  minusDay(date, day) {
    return new Date(date.getTime() - day * 24 * 60 * 60 * 1000)
  },

  timeBetween(startTime, endTime) {
    const d = {}
    d.mills = endTime - startTime // 毫秒数
    d.seconds = Math.floor((endTime - startTime) / 1000) // 秒数
    d.minutes = Math.floor((endTime - startTime) / 1000 / 60) // 分钟
    d.hours = Math.floor((endTime - startTime) / 1000 / 60 / 60) // 小时
    d.days = Math.floor((endTime - startTime) / 1000 / 60 / 60 / 24) // 天数
    return d
  },
}

// export const currentTime = () =>
//   new Date(2018, 5, 1, 11, 11, 11);

// define system time
// 系统时间定义，系统认为的当前时间点，从此点开始运行
// 该时间只能控制高炉，
// 烧结模块请求后端不带时间， 是取最新的值，所以模拟的时候，保证两端时间起点一致
// TODO 烧结请求带上时间戳
export const currentTime = () => new Date();

// date 转 字符串
export const dateFormat = (date, timeFormat = true) => {
  const seperator1 = '-';
  const seperator2 = ':';
  let month = date.getMonth() + 1;
  let day = date.getDate();
  let hours = date.getHours();
  let minutes = date.getMinutes();
  let seconds = date.getSeconds();
  if (month >= 1 && month <= 9) {
    month = `0${month}`
  }
  if (day >= 0 && day <= 9) {
    day = `0${day}`
  }
  if (hours >= 0 && hours <= 9) {
    hours = `0${hours}`
  }
  if (minutes >= 0 && minutes <= 9) {
    minutes = `0${minutes}`
  }
  if (seconds >= 0 && seconds <= 9) {
    seconds = `0${seconds}`
  }
  let currentdate = `${date.getFullYear() + seperator1 + month + seperator1 + day}`;
  currentdate = timeFormat ? currentdate + ` ${hours}${seperator2}${minutes}${seperator2}${seconds}` : currentdate;

  return currentdate
};

// 字符串 转 date
export const convertDateFromString = dateString => {
  if (dateString) {
    const a = dateString.substring(0, 19)
    const d = a.replace(/-/g, '/')

    return new Date(d)
  }
  return null;
};

export const getMockTime = async name => {
  const resp = await getLatestTime({
    itemName: name,
  });
  const {start, end, current} = resp.detail
  let startTime = convertDateFromString(start)
  let endTime = convertDateFromString(end)
  let currentTime = convertDateFromString(current)

  const payload = {
    start: startTime,
    end: endTime,
    current: currentTime
  };
  return payload
}

